<?php
namespace Ekadmin\Controller;

use \Think\Controller;
use Think\Page;

class GoodsCategoryController extends CommonController
{

    /**
     * *@商品分类列表显示页面
     */
    public function index()
    {
        $where['uniacid'] = $this->uniacid;
        $obj = M('goodsclass');
        $count = $obj->where($where)->count();
        $list = $obj->order('sort desc')->where($where)->select();
        
        //$list = getMenu($list);
        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * *@商品分类修改页面显示
     */
    public function edit()
    {
        $data = M('goodsclass')->where("id=".I("id")." and uniacid=".$this->uniacid)->find();
        $this->assign('data', $data);
        $this->assign('list', $this->getTopmenu());
        $this->display('form');
    }

    /**
     * *@获取商品一级分类函数
     */
    private function getTopmenu()
    {
        $map['pid'] = 0;
        
        $map["uniacid"]=$this->uniacid;
        
        return M('goodsclass')->where($map)->getField('id,name');
    }

    /**
     * *@显示添加商品分类页面
     */
    public function add()
    {
        $this->assign('list', $this->getTopmenu());
        $this->display('form');
    }

    /**
     * *@修改商品分类提交操作
     */
    public function update()
    {
        if (IS_POST) {
            $param = I('post.');
            if (! $param['name']) {
                ajax_return(1, '请填写分类名称');
            }
            if (! $param['icon']) {
                ajax_return(1, '请上传图标');
            }
            $param["sort"] = intval($param["sort"]);
            $obj = M('goodsclass');
            if ($param['id']) {
                $obj->save($param);
            } else {
                $param['addtime'] = time();
                $param['uniacid'] = $this->uniacid;
                $obj->add($param);
            }
            ajax_return(0, '保存成功', U('index'));
        }
    }

    /**
     * *@删除商品分类操作
     */
    public function del()
    {
        if (IS_POST) {
            $param = I('id');
            if (is_array($param)) {
                $map['id'] = array("in",$param);
            } else {
                $map['id'] = $param;
            } 
            // if(M('goodsclass')->where(['pid'=>$param])->find()){
            // ajax_return(1,'该分类下有子分类,请先删除子分类');
            // }
            $map["uniacid"]=$this->uniacid;
            
            M('goodsclass')->where($map)->delete();
            ajax_return(0, '删除成功');
        }
    }
}